import { createRouter, createWebHistory } from 'vue-router'
const HomeView = () => import('@/views/HomeView.vue')
const UserLoginView = () => import('@/views/UserLoginView')
const RegisteredView = () => import ('@/views/RegisteredView');

const homeContent = () => import ('@/sidePages/HomeContent.vue');
const SignContent = () => import ('@/sidePages/SignContent.vue');
const ReadContent = () => import ('@/sidePages/ReadContent.vue');
const AboutContent = () => import ('@/sidePages/AboutContent.vue');
const CouponsContent = () => import ('@/sidePages/CouponsContent.vue');

const routes = [
  {
    path: '/',
    name: 'userlogin',
    component:UserLoginView,
    meta:{
      title: '智能作平台登录'
    }
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    redirect:{name:"homeContent"},
    meta:{
      title: '智能作平台首页',
      authRequired: true
    },
    children: [{
      path:'/homeContent',
      name:'homeContent',
      component: homeContent
    },{
      path:'/signContent',
      name:'signContent',
      component: SignContent
    },{
      path:'/readContent',
      name:'readContent',
      component: ReadContent
    },{
      path:'/couponsContent',
      name:'couponsContent',
      component: CouponsContent
    },{
      path:'/aboutContent',
      name:'aboutContent',
      component: AboutContent
    }]
  },
  {
    path: '/registered',
    name: 'registered',
    component: RegisteredView,
    meta:{
      title: '智能作平台注册'
    }
  },
  // {
  //   path: '/adminlogin',
  //   name: 'adminlogin',
  //   component:AdminLoginView,
  //   meta:{
  //     title: '智能作平台管理员登录'
  //   }
  // }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 前置钩子
router.beforeEach((to,from,next) => {
  // 从from跳转到to
  document.title = to.meta.title
  
  const token = localStorage.getItem('token')
  if(to.name !== 'userlogin' && to.name !== 'registered' && !token)
  next({name:'userlogin'})
  else next()
})

export default router
